﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using DatabaseLayer;
using System.Data.Linq;


namespace ControlLayer
{
    public class CustomerCtr : ICustomerCtr
    {

        static TablesDataContext db = new TablesDataContext();

        public void CreateCustomer(string ssn, string creditNo, string name, string street, int house, int zip, int phoneNo, string email, string pass)
        {
            Customer cust = new Customer();
            cust.ssn = ssn;
            cust.creditNo = creditNo;
            cust.name = name;
            cust.street = street;
            cust.house = house;
            cust.ZipCode = zip;
            cust.phone = phoneNo;
            cust.email = email;
            cust.pass = pass;

            db.Customers.InsertOnSubmit(cust);
            db.SubmitChanges();
        }

        public Customer GetCustomer(string ssn)
        {
            IQueryable<Customer> custQue =
                    from cust in db.Customers
                    where cust.ssn.Equals(ssn)
                    select cust;

            //Customer c = custQue.ToList()[0];
            //return new Customer { creditNo = c.creditNo, email = c.email, name = c.name, house = c.house, Orders = c.Orders, pass = c.pass, phone = c.phone, ssn = c.ssn, street = c.street, ZipCode = c.ZipCode };
            return custQue.ToList()[0];
        }

        public List<Customer> GetCustomers(string name)
        {
            IQueryable<Customer> custQue =
                from cust in db.Customers
                where cust.name.Contains(name)
                select cust;

            return custQue.ToList();
        }

        public void UpdateCustomer(string ssn, string creditNo, string name, string street, int house, int zip, int phoneNo, string email, string pass)
        {
            IQueryable<Customer> custQue =
                from cust in db.Customers
                where cust.ssn.Equals(ssn)
                select cust;

            foreach (Customer cust in custQue)
            {
                if(creditNo != null)
                cust.creditNo = creditNo;
                if(name != null)
                cust.name = name;
                if(street != null)
                cust.street = street;
                if(house != 0)
                cust.house = house;
                if(zip != 0)
                cust.ZipCode = zip;
                if(phoneNo != 0)
                cust.phone = phoneNo;
                if (email != null)
                cust.email = email;
                if (pass != null)
                cust.pass = pass;
            }
            db.SubmitChanges();
        }

        public void DeleteCustomer(string ssn)
        {
            Customer baybay = GetCustomer(ssn);
            db.Customers.DeleteOnSubmit(baybay);
            db.SubmitChanges();
        }

        //testmethod
        public string PassSomeText(string s)
        {
            return s;
        }

        //testmethod
        public Zip_City GetZipCity(string n)
        {
            IQueryable<Zip_City> zipQue =
               from zi in db.Zip_Cities
               where zi.city.Equals(n)
               select zi;

            return zipQue.ToList()[0];
        }




        //---Async Code Testing, ignore---

        //public IAsyncResult BeginGetCustomer(string ssn, AsyncCallback callback, object state)
        //{
        //    IQueryable<Customer> custQue =
        //        from cust in db.Customers
        //        where cust.ssn.Equals(ssn)
        //        select cust;

        //    return new CompletedAsyncResult<Customer>(custQue.ToList()[0]);
        //}

        //public Customer EndGetCustomer(IAsyncResult result)
        //{
        //    CompletedAsyncResult<Customer> r = result as CompletedAsyncResult<Customer>;
        //    return r.Data;
        //}

        //class CompletedAsyncResult<T> : IAsyncResult
        //{
        //    T data;

        //    public CompletedAsyncResult(T data)
        //    { this.data = data; }

        //    public T Data
        //    { get { return data; } }

        //    #region IAsyncResult Members
        //    public object AsyncState
        //    { get { return (object)data; } }

        //    public WaitHandle AsyncWaitHandle
        //    { get { throw new Exception("The method or operation is not implemented."); } }

        //    public bool CompletedSynchronously
        //    { get { return true; } }

        //    public bool IsCompleted
        //    { get { return true; } }
        //    #endregion
        //}

    }
}
